import mysql.connector
import urllib.request
import http.cookiejar
import sys
import time
import os
import json
import hashlib
import base64
import execjs 

from html.parser import HTMLParser
from html.entities import name2codepoint

#时间格式化
TIMEFORMAT = '%Y-%m-%d %X'

web_url = 'https://www.ixigua.com/'
web_tag = '西瓜视频'
web_source = 'xigua'

print('**************西瓜视频start**************')
print('执行时间: %s'%(time.strftime(TIMEFORMAT, time.localtime())))

#ascp,crc32
ctx = execjs.compile("""

    function ascp(ts) {
        var t = Math.floor((new Date).getTime() / 1e3)
          , e = t.toString(16).toUpperCase()
          , n = ts.toString(32).toUpperCase().split('');
        if (8 != e.length)
            return {
                as: "479BB4B7254C150",
                cp: "7E0AC8874BB0985"
            };
        var e2 = e.split('');
        for (var r = n.slice(0, 5), o = n.slice(-5), i = "", s = 0; s < 5; s++)
            i += r[s] + e2[s];
        for (var u = "", l = 0; l < 5; l++)
            u += e2[l + 3] + o[l];
        return {
            as: "A1" + i + e.slice(-3),
            cp: e.slice(0, 3) + u + "E1"
        }
    }

    function crc32(hostname, pathname) {
        var e;
        var t = hostname + pathname;
        var n = function() {
            for (var t = 0, e = new Array(256), n = 0; 256 != n; ++n)
                t = n,
                t = 1 & t ? -306674912 ^ t >>> 1 : t >>> 1,
                t = 1 & t ? -306674912 ^ t >>> 1 : t >>> 1,
                t = 1 & t ? -306674912 ^ t >>> 1 : t >>> 1,
                t = 1 & t ? -306674912 ^ t >>> 1 : t >>> 1,
                t = 1 & t ? -306674912 ^ t >>> 1 : t >>> 1,
                t = 1 & t ? -306674912 ^ t >>> 1 : t >>> 1,
                t = 1 & t ? -306674912 ^ t >>> 1 : t >>> 1,
                t = 1 & t ? -306674912 ^ t >>> 1 : t >>> 1,
                e[n] = t;
            return "undefined" != typeof Int32Array ? new Int32Array(e) : e
        }()
          , o = function(t) {
            for (var e, o, r = -1, i = 0, a = t.length; i < a; )
                e = t.charCodeAt(i++),
                e < 128 ? r = r >>> 8 ^ n[255 & (r ^ e)] : e < 2048 ? (r = r >>> 8 ^ n[255 & (r ^ (192 | e >> 6 & 31))],
                r = r >>> 8 ^ n[255 & (r ^ (128 | 63 & e))]) : e >= 55296 && e < 57344 ? (e = (1023 & e) + 64,
                o = 1023 & t.charCodeAt(i++),
                r = r >>> 8 ^ n[255 & (r ^ (240 | e >> 8 & 7))],
                r = r >>> 8 ^ n[255 & (r ^ (128 | e >> 2 & 63))],
                r = r >>> 8 ^ n[255 & (r ^ (128 | o >> 6 & 15 | (3 & e) << 4))],
                r = r >>> 8 ^ n[255 & (r ^ (128 | 63 & o))]) : (r = r >>> 8 ^ n[255 & (r ^ (224 | e >> 12 & 15))],
                r = r >>> 8 ^ n[255 & (r ^ (128 | e >> 6 & 63))],
                r = r >>> 8 ^ n[255 & (r ^ (128 | 63 & e))]);
            return r ^ -1
        }
          , r = pathname + "?r=" + Math.random().toString(10).substring(2);
        "/" != r[0] && (r = "/" + r);
        var i = o(r) >>> 0 , a = 0;
        return (a ? ['http', hostname] : ["http:", hostname]).join("//") + r + "&s=" + i
    }
    """)

#获取视频列表
def getVideoList():
    try:
        nowtime = str(int(time.time()))
        md5 = hashlib.md5()
        md5.update(nowtime.encode("utf-8"))
        ts = str(md5.hexdigest())
        #print(ts)
        ascp = str(ctx.call("ascp", ts))
        #print(ascp)
        ascpJson = json.loads(ascp.replace("'", "\""))

        requestUrl = "https://www.ixigua.com/api/pc/feed/?min_behot_time=%d&category=video_new&utm_source=toutiao&widen=1&tadrequire=true&as=%s&cp=%s_signature=%s"%(0, ascpJson.get("as"), ascpJson.get("cp"), 'd7ItuRASLfG52atrdJIi0neyLa')
        print('请求地址：%s'%(requestUrl))
        #抓取瓜视频列表
        headers = {
            'Cache-Control': 'no-cache',
            'Accept': '*/*',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
            'Referer': 'https://www.ixigua.com/',
            'Connection': 'keep-alive',
            "Cookie": "tt_webid=6508983108954752516"
        }
        #cookie
        cookie = http.cookiejar.CookieJar()
        handler = urllib.request.HTTPCookieProcessor(cookie)
        opener = urllib.request.build_opener(handler)
        urllib.request.install_opener(opener)

        req = urllib.request.Request(requestUrl, headers=headers)
        f = urllib.request.urlopen(req)
        response = f.read()
        #print(response.decode('utf-8'))

        videoData = json.loads(response)
        videoList = videoData.get('data')
        return(videoList)
    except Exception as e:
        print(str(e))

#保存视频信息到数据库
def saveVideo(videoList):
    try:
        #连接数据库记录数据
        conn = mysql.connector.connect(host='10.0.160.19', port=3306,user='root',passwd='fzxhb001',db='haha_live')
        cursor = conn.cursor()
        #遍历西瓜视频列表
        for i, video in enumerate(videoList):
            if video.get('is_feed_ad') == True:
                continue
            behot_time = video.get('behot_time')
            video_id = video.get('video_id')
            title = video.get('title')
            source = video.get('source')
            image_url = video.get('image_url')
            video_play_count = video.get('video_play_count')
            video_duration_str = video.get('video_duration_str')
            comment_count = video.get('comments_count')
            if comment_count is None:
                comment_count = 0
            #获取视频地址
            requestUrl = str(ctx.call("crc32", "ib.365yg.com", "video/urls/v/1/toutiao/mp4/" + video_id))
            f = urllib.request.urlopen(requestUrl)
            response = f.read()
            #print(response)
            #
            json_dict = json.loads(response)

            #video_data_json
            video_data_json = json.dumps(video)
            video_data_json2 = json.dumps(json_dict)
            #
            if(json_dict.get('data').get('status') == 10):
                data = json_dict.get('data')
                #封面地址
                poster_url = data.get('poster_url')
                video_list = data.get('video_list')
                #视频时长
                video_duration = data.get('video_duration')
                #视频信息
                video_1 = video_list.get('video_1')
                vtype = video_1.get('vtype')
                vwidth = video_1.get('vwidth')
                vheight = video_1.get('vheight')
                bitrate = video_1.get('bitrate')
                size = video_1.get('size')
                video_url = base64.b64decode(video_1.get('main_url')).decode()
                #sql
                sql ="""INSERT INTO `live_video_collect` (`video_id`, `image_url`, `poster_url`, `title`, `source`, `video_url`, `video_duration`, `video_duration_format`, `video_play_count`, `video_width`, `video_height`, `video_size`, `comment_count`, `behot_time`, `video_data_json`, `video_data_json2`, `web_url`, `web_tag`, `web_source`, `create_time`) 
                VALUES ('%s', '%s', '%s', '%s', '%s', '%s', %s, '%s', %s, %s, %s, %s, %s, %s, '%s', '%s', '%s', '%s', '%s', UNIX_TIMESTAMP())""" % (video_id, image_url, poster_url, title, source, video_url, video_duration, video_duration_str, video_play_count, vwidth, vheight, size, comment_count, behot_time, video_data_json, video_data_json2, web_url, web_tag, web_source)
                #print(sql)
                try:
                    cursor.execute(sql)
                    conn.commit()
                    print('%s-->插入成功id=%d'%(web_tag, cursor.lastrowid));
                except Exception as ex:
                    print('异常sql: '%(sql))
                    print(str(ex))
        cursor.close()
        conn.close()
    except Exception as e:
        print(str(e))
        cursor.close()
        conn.close()

#执行调用
videoList = getVideoList()
saveVideo(videoList)
print('**************西瓜视频end**************')